﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCM.UI.Utils;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.UI;
using System.Windows.Browser;
using System.Text;


namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCM22 : EFViewCmes
    {
        IServiceAgent service;
        private EIBlock block;
        private MaterialTypeBlock materialblock = new MaterialTypeBlock();//构造前台下拉列表
        private EIInfo tempEIInfo;
        private bool IsNeedApprover = true;//是否需要审批

        private object Str  //得到当前记录计划状态
        {
            get { return this.grid.SelectedItem.GetType().GetProperty("fdPlanStatus").GetValue(this.grid.SelectedItem, null); }
        }
        
        public ZMCM22()
        {
            InitializeComponent();
            service = new HttpServiceAgent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            block = initData.GetBlock("cmes.cm.planType");
            if (initData.Attributes.ContainsKey("isNeedApprover"))
            {
                if ("0".Equals(initData.Get("isNeedApprover")))
                {
                    IsNeedApprover = false;
                }
            }
        }

        /// <summary>
        /// 主表选中记录发生改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (null != this.grid.SelectedItem)
            {
                //根据状态判断从表的按钮是否可用
                BtnVisibilityByStatus();

                EIInfo info = new EIInfo();
                info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCM23");
                info.Attributes.Add(EIConstants.MethodNameKey, "query");
                EIRow _row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();
                info.Attributes.Add("fdPlanId", _row["fdKeyId"].ToString());
                EIBlock block = new EIBlock("params");
                block.AddRow((this.grid.SelectedItem as UIBaseEntity).GetEIRow());
                info.AddBlock(block);
                info.AddBlock(EIConstants.ResultBlock).Set(EIConstants.ShowCount, "true");//如果不加这个，翻页的统计和显示是错误的
                service.CallService(info, OnCallChildServiceCompleted);
            }
            else
            {
                EIInfo info = new EIInfo();
                info.AddBlock("Planresult");
                this.ChildGrid.UpdateData(info);
            }
        }

        /// <summary>
        /// 根据状态判断从表的按钮是否可用
        /// </summary>
        private void BtnVisibilityByStatus()
        {
            if ("未上报".Equals(Str))
            {
                Button insert = this.FindName("PLANINSERT") as Button;
                if (insert != null)
                {
                    insert.IsEnabled = true;
                }
                Button update = this.FindName("PLANUPDATE") as Button;
                if (update != null)
                {
                    update.IsEnabled = true;
                }
                Button delete = this.FindName("PLANDELETE") as Button;
                if (delete != null)
                {
                    delete.IsEnabled = true;
                }
            }
            else
            {
                Button insert = this.FindName("PLANINSERT") as Button;
                if (insert != null)
                {
                    insert.IsEnabled = false;
                }
                Button update = this.FindName("PLANUPDATE") as Button;
                if (update != null)
                {
                    update.IsEnabled = false;
                }
                Button delete = this.FindName("PLANDELETE") as Button;
                if (delete != null)
                {
                    delete.IsEnabled = false;
                }
            }
        }

        /// <summary>
        /// 刷新从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCallChildServiceCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            tempEIInfo = e.EIInfo;//更新缓存
            this.ChildGrid.UpdateData(e.EIInfo);
        }

        #region 主表操作

        /// <summary>
        /// 主表复制上月操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnCopyplanButtonClick(object sender, RoutedEventArgs e)
        {
            base.TempButton = sender as Button;
            base.TempButton.IsEnabled = false;

            string parentid = this.fdParentId.Value.ToString();
            string year = parentid.Substring(0, 4);//得到当前年份
            string month = parentid.Substring(4);//得到当前月份

            if (int.Parse(month) == 1)
            {
                year = (int.Parse(year) - 1).ToString();
                month = "12";
            }
            else
            {
                month = (int.Parse(month) - 1).ToString();
            }

            parentid = year + month;

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCM22");
            eiInfo.Set("method", "query");
            eiInfo.Set("fdParentId", parentid);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoWorkCompleted);
        }

        /// <summary>
        /// 新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                OpenCopyWindow(inInfo, "上月需求计划--复制");
            }
        }

        /// <summary>
        /// 主表新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            base.TempButton = sender as Button;
            base.TempButton.IsEnabled = false;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCM23");
            eiInfo.Set("method", "query");
            eiInfo.Set("fdParentId",this.fdParentId.Value);
            base.OnInsertButtonClickPopup(eiInfo, "月计划新增", "insert", "Parent", sender, e);
        }

        /// <summary>
        /// 主表修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行修改！", true);
                return;
            }
            if (!this.grid.GetCheckedEIRows()[0]["fdPlanStatus"].ToString().Equals("未上报"))
            {
                base.OpenEFWindowAlert("月计划不是未上报状态，不允许修改！", true);
                return;
            }
            else 
            {
                EFViewCmesProperty _view = new EFViewCmesProperty();
                _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
                base.ViewProperty = null;
                base.ViewProperty = _view;
                IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
                EIInfo eiInfo = new EIInfo();
                EIBlock eIBlock = new EIBlock("result");
                EIRow row = new EIRow();
                if (_list.Count > 0)
                {
                    row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                }
                eIBlock.SetRow(0, row);
                eiInfo.AddBlock(eIBlock);
                base.OnUpdateButtonClickPopup(eiInfo, "月计划修改", "update", "Parent", sender, e);
            }
        }

        /// <summary>
        /// 主表删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行删除！", true);
                return;
            }
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = null;
            base.ViewProperty = _view;
            if (this.grid.GetCheckedEIRows()[0]["fdPlanStatus"].ToString().Equals("未上报"))
            {
                //if (this.ChildGrid.HasItems)
                //{
                //    base.OpenEFWindowAlert("明细表有记录，不可删除主表记录！", true);
                //    return;
                //}
                //else
                //{
                    base.OnDeleteButtonClick("ZMCM22", "delete", sender, e);
                //}
            }
            else
            {
                base.OpenEFWindowAlert("计划状态不是未上报状态，不允许删除！", true);
            }
        }

        /// <summary>
        /// 主表上报操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnReportButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行上报！", true);
                return;
            }
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = null;
            base.ViewProperty = _view;
            if (this.grid.GetCheckedEIRows()[0]["fdPlanStatus"].ToString().Equals("未上报"))
            {
                base.OnModifyButtonClick("ZMCM22", "report", "上报", sender, e);
            }
            else
            {
                base.OpenEFWindowAlert("计划状态不是未上报状态，不允许上报！", true);
            }
        }

        /// <summary>
        /// 主表撤回操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public  void OnQxreturnButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行撤回！", true);
                return;
            }
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = null;
            base.ViewProperty = _view;
            String str = this.grid.GetCheckedEIRows()[0]["fdPlanStatus"].ToString();
            String dqrId = this.EIDataContext.GetAttribute("userId").ToString();
            
            if (str.Equals("审批中") || str.Equals("已上报"))
            {
                base.OnModifyButtonClick("ZMCM22", "qxreturn", "撤回", sender, e);
            }
            else if (str.Equals("已审批"))
            {
                base.OpenEFWindowAlert("计划状态是已审批状态，不允许撤回！", true);
            }
            else if (str.Equals("已审核"))
            {
                base.OpenEFWindowAlert("计划状态是已审核状态，不允许撤回！", true);
            }
            else
            {
                base.OpenEFWindowAlert("计划状态是未上报状态，不允许撤回！", true);
            }
        }

        /// <summary>
        /// 主表打印操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPrintButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            String fdKeyId = "";
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择记录进行打印！", true);
                return;
            }
            else
            {
                if(count > 1)
                {
                    this.OpenEFWindowAlert("只能选择一条记录进行打印！", true);
                    return;
                }
                fdKeyId = "'" + this.grid.GetCheckedEIRows()[0]["fdKeyId"].ToString() + "'";
            }
            Uri uri = Uploader.ConstructAbsoluteUri("../admin/RunmaxReport.jsp?projectName=CMESReport&reportName=TeamMaterialPlanReprot&KeyId=" + fdKeyId);
            HtmlPage.Window.Navigate(uri, "_blank");
            base.TempButtonOpen(true);//按钮生效
        }

        ///// <summary>
        ///// 主表审批操作
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //public void OnApproveButtonClick(object sender, RoutedEventArgs e)
        //{
        //    //按钮失效
        //    Button _button = sender as Button;
        //    _button.IsEnabled = false;
        //    base.TempButton = _button;
        //    int count = this.grid.GetCheckedEIRows().Count;
        //    if (count <= 0)
        //    {
        //        this.OpenEFWindowAlert("请选择一条记录进行审批！", true);
        //        return;
        //    }
        //    EFViewCmesProperty _view = new EFViewCmesProperty();
        //    _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
        //    base.ViewProperty = null;
        //    base.ViewProperty = _view;
        //    String str = this.grid.GetCheckedEIRows()[0]["fdPlanStatus"].ToString();
        //    String dqrId = this.EIDataContext.GetAttribute("userId").ToString();

        //    if (dqrId.Equals(this.grid.GetCheckedEIRows()[0]["fdApprovePeople"]))
        //    {
        //        if (str.Equals("已上报") || str.Equals("审批中"))
        //        {
        //            base.OnModifyButtonClick("ZMCM22", "approve", "审批", sender, e);
        //        }
        //        else
        //        {
        //            base.OpenEFWindowAlert("计划状态不是已上报或审批中状态，不允许审批！", true);
        //        }
        //    }
        //    else
        //    {
        //        base.OpenEFWindowAlert("无审批权限！", true);
        //    }
        //}

        ///// <summary>
        ///// 主表审核操作
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //public void OnAuditButtonClick(object sender, RoutedEventArgs e)
        //{
        //    //按钮失效
        //    Button _button = sender as Button;
        //    _button.IsEnabled = false;
        //    base.TempButton = _button;
        //    int count = this.grid.GetCheckedEIRows().Count;
        //    if (count <= 0)
        //    {
        //        this.OpenEFWindowAlert("请选择一条记录进行审核！", true);
        //        return;
        //    }
        //    EFViewCmesProperty _view = new EFViewCmesProperty();
        //    _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
        //    base.ViewProperty = null;
        //    base.ViewProperty = _view;
        //    string str = this.grid.GetCheckedEIRows()[0]["fdPlanStatus"].ToString();
        //    string dqdwId = this.EIDataContext.GetAttribute("orgCode").ToString();
        //    if (dqdwId.Equals(this.grid.GetCheckedEIRows()[0]["fdAuditDepartment"]))
        //    {
        //        if (str.Equals("已审批"))//只有已审批状态才可以进行审核
        //        {
        //            base.OnModifyButtonClick("ZMCM22", "audit", "审核", sender, e);
        //        }
        //        else
        //        {
        //            base.OpenEFWindowAlert("计划状态不是已审批状态，不允许审核！", true);
        //        }
        //    }
        //    else
        //    {
        //        base.OpenEFWindowAlert("无审核权限！", true);
        //    }

        //}

        /// <summary>
        /// 主表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ParentInsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
            }
            base.TempButtonOpen(true);//按钮生效
        }

        #endregion

        #region 从表操作

        /// <summary>
        /// 从表新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPlanInsertButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            if (this.grid.SelectedItem == null)
            {
                base.OpenEFWindowAlert("请在主表先选择一条或者录入一条记录！", true);
                return;
            }

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCM23");
            eiInfo.Set("method", "querySerialNumber");
            eiInfo.Set("fdPlanId", this.grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.grid.SelectedItem, null));

            HttpServiceAgent service = new HttpServiceAgent();
            service.CallService(eiInfo, OnCallServiceCompleted);
        }

        private void OnCallServiceCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eiInfo = e.EIInfo;
            object dqdwId = this.EIDataContext.GetAttribute("orgCode").ToString();
            object dqrId = this.EIDataContext.GetAttribute("userId").ToString();

            this.ChildOpenPopup(eiInfo, "月计划明细", "insert");
        }

        /// <summary>
        /// 从表修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPlanUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.ChildGrid.GetCheckedEIRows();
            base.ViewProperty = null;
            base.ViewProperty = _view;
            IList<EIRow> _list = this.ChildGrid.GetCheckedEIRows() == null ? new List<EIRow>() : this.ChildGrid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("Planresult").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "月计划明细", "update", "Child", sender, e);          
       }

        /// <summary>
        /// 从表删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPlanDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.ChildGrid.GetCheckedEIRows();
            base.ViewProperty = null;
            base.ViewProperty = _view;
            EIInfo eiInfo = new EIInfo();
            this.ChildGrid.EIBlock.Set("fdPlanStatus", Str);
            object dqdwId = this.EIDataContext.GetAttribute("orgCode").ToString();
            object dqrId = this.EIDataContext.GetAttribute("userId").ToString();

            base.OnDeleteButtonClick("ZMCM23", "delete", sender, e);            
        }

        /// <summary>
        /// 从表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChildInsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.ChildGrid.Refresh();//刷新从表Grid区域
                UpdateGridData();//刷新主表Grid区域
            }
            base.TempButtonOpen(true);//按钮生效
        }
        #endregion

        /// <summary>
        /// 包括主表、从表
        /// 新增、修改、查看之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            if (_args.ToLower() == "parent")
            {
                this.ParentOpenPopup(_eIInfo, _title, _dialogType);
            }
            else
            {
                this.ChildOpenPopup(_eIInfo, _title, _dialogType);
            }
        }
        
        /// <summary>
        /// 主表新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public void ParentOpenPopup(EIInfo _eIInfo, string _title, string _dialogType)
        {
            TeamMaterialMonthPlan zmcm16 = new TeamMaterialMonthPlan(_dialogType);//构造一个新的弹出页面
            zmcm16.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.people"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.planType"));
            zmcm16.EIDataContext.Update(_eIInfo);
            zmcm16.fdParentId.Value = this.fdParentId.Value;//当前节点ID
            zmcm16.fdParentName.Value = this.fdParentName.Value;//当前节点名称
            zmcm16.isLeaf.Value = this.isLeaf.Value;//是否叶子
            String parentid = "";
            //((zmcm16.fdApprovePeople.Content) as CmesCustomView).eViewInitParams = "bindingService=ZM03.query,columns=empCode|empName|orgName,queryConditions=empCode|empName,initParam=orgCode:" + this.EIDataContext.GetAttribute("orgCode").ToString();
            if ("insert".Equals(_dialogType))
            {
                parentid = this.fdParentId.Value.ToString();
                zmcm16.fdYear.Value = parentid.Substring(0,4);//得到当前年份
                zmcm16.fdMonth.Value = parentid.Substring(4);//得到当前月份
                zmcm16.fdApplicationDate.Value = DateTime.Now.ToString("yyyy-MM-dd");//得到当前日期
                zmcm16.fdApplicationDeparment.Value = this.EIDataContext.GetAttribute("orgCode").ToString();//得到当前登录用户所属单位
                zmcm16.fdAuditDepartment.Value = this.EIDataContext.GetAttribute("fdAuditDepartmentId").ToString();//审核单位ID
            }

            if (!IsNeedApprover)
            {
                zmcm16.fdApprovePeople.Visibility = Visibility.Collapsed;
                zmcm16.fdApprovePeople.eRegex = null;
            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = zmcm16;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(ParentInsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 从表新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public void ChildOpenPopup(EIInfo _eIInfo, string _title, string _dialogType)
        {
            TeamMaterialPlanDetail zmcm15 = new TeamMaterialPlanDetail(_dialogType);//构造一个新的修改页面
            zmcm15.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.financeClassify"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.materoalName"));
            _eIInfo.AddBlock(materialblock.GetMaterialType());  //物资分类下拉框           
            zmcm15.EIDataContext.Update(_eIInfo);
            EIRow _row = this.grid.EIBlock.GetRow(this.grid.SelectedIndex);
            if ("insert".Equals(_dialogType))
            {
                zmcm15.fdPlanId.Value = _row["fdKeyId"];//主表ID
                zmcm15.fdAuditNum.Value = "0";
                zmcm15.fdPurchaseNum.Value = "0";
                zmcm15.fdSerialNum.Value = _eIInfo.Get("fdSerialNum");

                EIBlock block = this.EIDataContext.GetBlock("cmes.cm.financeClassify");
                if (block.GetRowCount() > 0)
                {
                    (zmcm15.fdFinanceClassify.Content as EFComboBox).SelectedIndex = 0;
                }
            }
            if (_dialogType == "update")
            {
                EIRow row_type = this.ChildGrid.GetCheckedEIRows()[0];
                if (row_type["fdMaterialType"].ToString().Equals("无编码物资"))
                {
                    zmcm15.fdMaterialName.IsReadOnly = false;

                    zmcm15.fdSpecModel.IsReadOnly = false;
                    zmcm15.fdSpecModel.eLabel = "*规格型号";
                    zmcm15.fdSpecModel.eRegex = "^.{1,50}$";
                    zmcm15.fdSpecModel.eValidateErrorPrompt = "输入1-50个字符";

                    zmcm15.fdParameter.IsReadOnly = false;
                    zmcm15.fdQuality.IsReadOnly = false;
                    zmcm15.fdNumber.IsReadOnly = false;

                    zmcm15.fdPrice.IsReadOnly = false;
                    zmcm15.fdPrice.eLabel = "*单价(元)";
                    zmcm15.fdPrice.eRegex = "((^\\d{1,6}$)|(^\\d{1,6}\\.\\d{1,5}$)|(^0\\.\\d{1,5}$))";
                    zmcm15.fdPrice.eValidateErrorPrompt = "输入整数位不超过6位，小数位不超过5位的数字";

                    zmcm15.fdTypeCode.eLabel = "*物资分类编码";
                    zmcm15.fdTypeCode.eRegex = "^.+$";
                    zmcm15.fdTypeCode.eValidateErrorPrompt = "不能为空";
                    zmcm15.fdTypeCode.IsEnabled = true;

                    zmcm15.fdMetric.eLabel = "*计量单位";
                    zmcm15.fdMetric.eRegex = "^.+$";
                    zmcm15.fdMetric.eValidateErrorPrompt = "不能为空";
                    zmcm15.fdMetric.IsEnabled = true;
                }
                (zmcm15.fdTypeCode.Content as CmesCustomView).Display =
                            row_type["fdTypeCode"].ToString() + row_type["fdTypeName"].ToString();
            }
            zmcm15.fdPlanStatus.Value = _row["fdPlanStatus"];      //计划状态

            CmesWindows _popup = new CmesWindows();         //构造一个弹出页面
            _popup.MinHeight = 200;
            _popup.MinWidth = 300;
            _popup.ResizeMode = ResizeMode.NoResize;    //设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = zmcm15;                  //让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(ChildInsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        private void OpenCopyWindow(EIInfo _eIInfo, string _title)
        {
            TeamMaterialMonthPlanCopy zmcm24 = new TeamMaterialMonthPlanCopy();//构造一个新的修改页面s

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.people"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.planType"));
            zmcm24.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();         //构造一个弹出页面
            _popup.MinHeight = 200;
            _popup.MinWidth = 300;
            _popup.ResizeMode = ResizeMode.NoResize;    //设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = zmcm24;                  //让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(ParentInsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 执行删除后的页面操作
        /// 包括主表删除和从表删除
        /// </summary>
        public override void DoDeleteOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            base.DoDeleteOperationCompleted(sender, e);
            this.OnQueryButtonClick(null, null);
            if (this.grid.SelectedIndex == -1 || this.grid.SelectedItem == null)
            {
                EIInfo info = new EIInfo();
                info.AddBlock("Planresult");
                this.ChildGrid.UpdateData(info);
            }
        }

        public void UpdateGridData()
        {
            if (null != this.grid.SelectedItem)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCM22");
                eiInfo.Set("method", "queryAfterUpdate");
                string fdKeyId = this.grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.grid.SelectedItem, null) as string;
                eiInfo.Set("fdKeyId", fdKeyId);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, UpdateStatisticsData);
            }
        }

        /// <summary>
        /// 更新统计数据
        /// </summary>
        private void UpdateStatisticsData(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Get("fdPlanSum") == null)
            {
                return;
            }
            else
            {
                EIRow row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();
                row["fdPlanSum"] = e.EIInfo.Get("fdPlanSum");
                row["fdMaterialSum"] = e.EIInfo.Get("fdMaterialSum");
            }
        }
    }
}
